package ch.fhnw.edu.rental.daos.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import ch.fhnw.edu.rental.daos.PriceCategoryDAO;
import ch.fhnw.edu.rental.model.PriceCategory;


public class JpaPriceCategoryDAO implements PriceCategoryDAO, ManagedDAO<PriceCategory> {

	@PersistenceContext
	private EntityManager em;
	
	@Override
	public PriceCategory getById(Long id) {
		return this.em.find(PriceCategory.class, id);
	}

	@Override
	public List<PriceCategory> getAll() {
		Query q = this.em.createQuery("from PriceCategory m");
		return q.getResultList();
	}

	@Override
	public void saveOrUpdate(PriceCategory priceCategory) {
		if (null == priceCategory.getId()) {
			this.em.persist(priceCategory);
		} else {
			this.em.merge(priceCategory);
		}
	}

	@Override
	public void delete(PriceCategory priceCategory) {
		this.em.remove(priceCategory);
	}
	
	@Override
	public PriceCategory manage(PriceCategory instance) {
		return em.merge(instance);
	}
}
